package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.product.mapper.SkuImageMapper;
import com.atguigu.gmall.product.service.SkuImageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * sku图片表service层实现类
 */
@Service
public class SkuImageServiceImpl implements SkuImageService {

    @Autowired
    private SkuImageMapper skuImageMapper;

    /**
     * 保存数据
     *
     * @param skuId
     * @param skuImageList
     */
    @Override
    public void save(Long skuId, List<SkuImage> skuImageList) {
        //遍历保存
        skuImageList.stream().forEach(skuImage -> {
            //补全数据
            skuImage.setSkuId(skuId);
            int insert = skuImageMapper.insert(skuImage);
            if(insert <= 0){
                throw new GmallException("新增sku图片表数据失败",null);
            }
        });
    }

    /**
     * 根据skuId查询商品对应的图片
     *
     * @param skuId
     * @return
     */
    @Override
    public List<SkuImage> listBySkuId(Long skuId) {
        return skuImageMapper.selectList(
                new LambdaQueryWrapper<SkuImage>()
                        .eq(SkuImage::getSkuId,skuId));
    }
}
